unit UBoletoBradesco;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RLReport, jpeg, RLBarcode, DB, DBClient, RLFilters,
  RLDraftFilter, RLRichFilter, RLHTMLFilter, RLXLSFilter, RLPDFFilter;

type
  TFBoletoBradesco = class(TForm)
    RLReport1: TRLReport;
    a: TRLBand;
    RLBarcodeCodigoBarras: TRLBarcode;
    RLImage2: TRLImage;
    RLLabel34: TRLLabel;
    RLDraw17: TRLDraw;
    RLDraw18: TRLDraw;
    RLLabel35: TRLLabel;
    RLDraw19: TRLDraw;
    RLDraw20: TRLDraw;
    RLLabel36: TRLLabel;
    RLLabel37: TRLLabel;
    RLLabel38: TRLLabel;
    RLLabel39: TRLLabel;
    RLDraw21: TRLDraw;
    RLDraw22: TRLDraw;
    RLDraw23: TRLDraw;
    RLLabel40: TRLLabel;
    RLLabel41: TRLLabel;
    RLLabel42: TRLLabel;
    RLDBText7: TRLDBText;
    RLDraw24: TRLDraw;
    RLLabel43: TRLLabel;
    RLLabel44: TRLLabel;
    RLDraw25: TRLDraw;
    RLLabel45: TRLLabel;
    RLDBText8: TRLDBText;
    RLDraw26: TRLDraw;
    RLLabel46: TRLLabel;
    RLLabel47: TRLLabel;
    RLDraw27: TRLDraw;
    RLLabel48: TRLLabel;
    RLLabel49: TRLLabel;
    RLDraw28: TRLDraw;
    RLLabel50: TRLLabel;
    RLLabel51: TRLLabel;
    RLDraw29: TRLDraw;
    RLLabel52: TRLLabel;
    RLDraw30: TRLDraw;
    RLLabel53: TRLLabel;
    RLLabel54: TRLLabel;
    RLLabel55: TRLLabel;
    RLDBText9: TRLDBText;
    RLLabel56: TRLLabel;
    RLLabel57: TRLLabel;
    RLDraw31: TRLDraw;
    RLLabel58: TRLLabel;
    RLDraw32: TRLDraw;
    RLLabel59: TRLLabel;
    RLDraw33: TRLDraw;
    RLImage3: TRLImage;
    RLLabel60: TRLLabel;
    RLMemo1: TRLMemo;
    RLLabel61: TRLLabel;
    RLDraw34: TRLDraw;
    RLDBText10: TRLDBText;
    RLLabel62: TRLLabel;
    RLDraw35: TRLDraw;
    RLLabel63: TRLLabel;
    RLDBText11: TRLDBText;
    RLDraw36: TRLDraw;
    RLLabel64: TRLLabel;
    RLDraw37: TRLDraw;
    RLLabel66: TRLLabel;
    RLDBText12: TRLDBText;
    RLDraw38: TRLDraw;
    RLLabel67: TRLLabel;
    RLDraw39: TRLDraw;
    RLDraw40: TRLDraw;
    RLLabel68: TRLLabel;
    RLDraw41: TRLDraw;
    RLLabel69: TRLLabel;
    RLDraw42: TRLDraw;
    RLLabel70: TRLLabel;
    RLDraw43: TRLDraw;
    RLLabel71: TRLLabel;
    RLLabel72: TRLLabel;
    RLLabel73: TRLLabel;
    RLLabel74: TRLLabel;
    RLLabel75: TRLLabel;
    RLDBText13: TRLDBText;
    RLDBText14: TRLDBText;
    RLDBText15: TRLDBText;
    RLDBText16: TRLDBText;
    RLLabel76: TRLLabel;
    RLDraw44: TRLDraw;
    RLLabel77: TRLLabel;
    RLLabel78: TRLLabel;
    RLDraw1: TRLDraw;
    RLImage1: TRLImage;
    RLLabel1: TRLLabel;
    RLDraw2: TRLDraw;
    RLLabel2: TRLLabel;
    RLDraw3: TRLDraw;
    RLDraw4: TRLDraw;
    RLLabel4: TRLLabel;
    RLLabel5: TRLLabel;
    RLLabel6: TRLLabel;
    RLDraw5: TRLDraw;
    RLLabel7: TRLLabel;
    RLDBText1: TRLDBText;
    RLDraw6: TRLDraw;
    RLLabel9: TRLLabel;
    RLDBText3: TRLDBText;
    RLDraw7: TRLDraw;
    RLDraw14: TRLDraw;
    RLDraw15: TRLDraw;
    RLDraw16: TRLDraw;
    RLLabel28: TRLLabel;
    RLDraw45: TRLDraw;
    RLLabel30: TRLLabel;
    RLDBText6: TRLDBText;
    RLDraw46: TRLDraw;
    RLLabel31: TRLLabel;
    RLDraw47: TRLDraw;
    RLDraw48: TRLDraw;
    RLLabel32: TRLLabel;
    RLDraw49: TRLDraw;
    RLLabel33: TRLLabel;
    RLDraw50: TRLDraw;
    RLLabel79: TRLLabel;
    RLDraw51: TRLDraw;
    RLLabel80: TRLLabel;
    RLMemo2: TRLMemo;
    RLLabel81: TRLLabel;
    RLDBText17: TRLDBText;
    RLDBText18: TRLDBText;
    RLDBText19: TRLDBText;
    RLDBText20: TRLDBText;
    RLLabel82: TRLLabel;
    RLLabel83: TRLLabel;
    RLDraw52: TRLDraw;
    RLLabel84: TRLLabel;
    RLLabel8: TRLLabel;
    RLDBText2: TRLDBText;
    RLLabel10: TRLLabel;
    RLLabel11: TRLLabel;
    RLDBText4: TRLDBText;
    RLLabel12: TRLLabel;
    RLLabel13: TRLLabel;
    RLLabel14: TRLLabel;
    RLDraw8: TRLDraw;
    RLLabel15: TRLLabel;
    RLLabel16: TRLLabel;
    RLLabel17: TRLLabel;
    RLLabel18: TRLLabel;
    RLDraw9: TRLDraw;
    RLLabel19: TRLLabel;
    RLDraw10: TRLDraw;
    RLLabel20: TRLLabel;
    RLLabel21: TRLLabel;
    RLLabel22: TRLLabel;
    RLDBText5: TRLDBText;
    RLLabel23: TRLLabel;
    RLLabel24: TRLLabel;
    RLDraw11: TRLDraw;
    RLLabel25: TRLLabel;
    RLDraw12: TRLDraw;
    RLLabel26: TRLLabel;
    RLDraw13: TRLDraw;
    RLLabel27: TRLLabel;
    DSBoleto: TDataSource;
    CDSBoleto: TClientDataSet;
    RLDBText21: TRLDBText;
    RLDBText22: TRLDBText;
    RLDBText23: TRLDBText;
    RLPDFFilter1: TRLPDFFilter;
    RLXLSFilter1: TRLXLSFilter;
    RLHTMLFilter1: TRLHTMLFilter;
    RLRichFilter1: TRLRichFilter;
    RLDraftFilter1: TRLDraftFilter;
    procedure CriaCDS;
    procedure RLLabel44BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);
    procedure RLLabel51BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);
    procedure RLMemo1BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);
    procedure RLMemo2BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);
    procedure RLDBText14BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);
    procedure RLDBText18BeforePrint(Sender: TObject; var Text: String;
      var PrintIt: Boolean);

   private
    { Private declarations }
  public

    procedure MontaBoleto(Conta,Titulo: String);

    { Public declarations }
  end;
var
  FBoletoBradesco: TFBoletoBradesco;
      MSN : sTRING; 

implementation

uses UGeral,  UDmGestao;

{$R *.dfm}

{ TFBoletoBradesco }

procedure TFBoletoBradesco.MontaBoleto(Conta, Titulo: String);
const ctSQLConta  = ' SELECT * FROM CONTACORRENTE WHERE CONT_CORR_CODIGO = %s ';

      ctSQLTitulo = ' SELECT                                                       '+#10+
                    '   CONTASRECEBER.*,                                           '+#10+
                    '   EMPRESA.*,                                                 '+#10+
                    '   CLIENTE.*                                                  '+#10+

                    ' FROM CONTASRECEBER                                           '+#10+

                    '   INNER JOIN EMPRESA ON                                      '+#10+
                    '           (EMPRESA.EMP_CODIGO = CONTASRECEBER.CODIGOEMPRESA) '+#10+

                    '   INNER JOIN CLIENTE ON                                      '+#10+
                    '           (CLIENTE.CLI_CODIGO = CONTASRECEBER.CR_CLIENTE)    '+#10+

                    ' WHERE                                                        '+#10+
                    '   (CONTASRECEBER.CR_CODIGO = %s)                             '+#10;

VAR contaNumero,contaNumeroDigito,contaAgencia,contaAgenciaDigito,contaBanco,contaCarteira : String;
    empresa,codEmpresa,venctoCR,cliente,clienteNome,clienteEndereco1,clienteEndereco2,codCR: String;
    valorCR : Real;
    agenciaCodCendente,nossoNumero : String;
    CodBarrasNumerico,CodBarras : String;
    campoLivre : String;
    DVCodigoBarras : String;
    c1,c2,c3,c4,c5 : String;
    doc : String;
    cnpj : String;
    TIPOCLIENTE : STRING;

begin



  Usando_Query_Pesquisa(Format(ctSQLConta,[Conta]));

  contaNumero       := copy(Query_Pesquisa.fieldbyname('NUMERO_CONTA').AsString,1,length(Query_Pesquisa.fieldbyname('NUMERO_CONTA').AsString) - 1);
  contaNumeroDigito := copy(Query_Pesquisa.fieldbyname('NUMERO_CONTA').AsString,length(Query_Pesquisa.fieldbyname('NUMERO_CONTA').AsString) - 1,1);

  contaAgencia       := copy(Query_Pesquisa.fieldbyname('NUMERO_AGENCIA').AsString,1,length(Query_Pesquisa.fieldbyname('NUMERO_AGENCIA').AsString) - 1);
  contaAgenciaDigito := copy(Query_Pesquisa.fieldbyname('NUMERO_AGENCIA').AsString,length(Query_Pesquisa.fieldbyname('NUMERO_AGENCIA').AsString) - 1,1);

  MSN := Trim(tRIM(Query_Pesquisa.fieldbyname('MENSAGEM1').AsString) + ' ' +
              tRIM(Query_Pesquisa.fieldbyname('MENSAGEM2').AsString) + ' ' +
              tRIM(Query_Pesquisa.fieldbyname('MENSAGEM3').AsString) + ' ' +
              tRIM(Query_Pesquisa.fieldbyname('MENSAGEM4').AsString));

  agenciaCodCendente := contaAgencia + '-' + contaAgenciaDigito + '/' + contaNumero + '-' + contaNumeroDigito;

  contaBanco    := Formata_Tamanho_String(Query_Pesquisa.fieldbyname('COD_BANCO_CAM_COM').AsString,3,'E','0');
  contaCarteira := Query_Pesquisa.fieldbyname('CODIGO_CARTEIRA').AsString;

  Usando_Query_Pesquisa(Format(ctSQLTitulo,[Titulo]));

  codEmpresa := Formata_Tamanho_String(Query_Pesquisa.fieldbyname('EMP_CODIGO').AsString,2,'E','0');
  codCR      := Formata_Tamanho_String(Query_Pesquisa.fieldbyname('CR_CODIGO').AsString,9,'E','0');

  doc := Query_Pesquisa.fieldbyname('CR_DOCUMENTO').AsString;

  nossoNumero := codEmpresa + codCR;

  empresa     := Query_Pesquisa.fieldbyname('EMP_RAZAO'        ).AsString;
  venctoCR    := Query_Pesquisa.fieldbyname('CR_DATAVENCIMENTO').AsString;
  valorCR     := Query_Pesquisa.fieldbyname('CR_VALOR'         ).AsFloat;
  clienteNome := Query_Pesquisa.fieldbyname('CLI_RAZAO'        ).AsString;
  cnpj        := Query_Pesquisa.fieldbyname('CLI_CPFCNPJ'        ).AsString;
  TIPOCLIENTE := Query_Pesquisa.fieldbyname('CLI_TIPOCLIENTE'        ).AsString;
  if Query_Pesquisa.Fieldbyname('CLI_IDENTENDBLOQUETE').AsInteger = 1 then
  begin
    clienteEndereco1 := Trim(Query_Pesquisa.FieldByName('CLI_ENDERECOCORRESP').AsString);
    clienteEndereco2 := Trim(Trim('CEP - ' + Query_Pesquisa.FieldByName('CLI_CEPCORRESP').AsString) + '   '+
                        Trim(Query_Pesquisa.FieldByName('CLI_BAIRROCORRESP').AsString) + '   ' +
                        Trim(Query_Pesquisa.FieldByName('CLI_CIDADECORRESP').AsString) + '   ' +
                        Trim(Query_Pesquisa.FieldByName('CLI_ESTADOCORRESP').AsString));
  end else begin
    clienteEndereco1 := Trim(Query_Pesquisa.FieldByName('CLI_ENDERECOPEDIDO').AsString);
    clienteEndereco2 := Trim(Trim('CEP - ' + Query_Pesquisa.FieldByName('CLI_CEPPEDIDO').AsString) + '   '+
                        Trim(Query_Pesquisa.FieldByName('CLI_BAIRROPEDIDO').AsString) + '   ' +
                        Trim(Query_Pesquisa.FieldByName('CLI_CIDADEPEDIDO').AsString) + '   ' +
                        Trim(Query_Pesquisa.FieldByName('CLI_ESTADOPEDIDO').AsString));
  end;

  campoLivre := contaAgencia +
                Formata_Tamanho_String(contaCarteira,2,'E','0') +
                nossoNumero +
                Formata_Tamanho_String(contaNumero,7,'E','0') +
                '0';

  CodBarras := contaBanco +
               '9' +
               Formata_Tamanho_String(StringReplace(StringReplace(formatFloat('0.,00',valorCR),'.','',[]),',','',[]),14,'E','0') +
               campoLivre;

  DVCodigoBarras := Mod11(CodBarras,9);

  if ((DVCodigoBarras = '0') or (DVCodigoBarras = '1') or (Strtoint(DVCodigoBarras) > 9)) then
    DVCodigoBarras := '1';

  insert(DVCodigoBarras,CodBarras,5);

  c1 := contaBanco + '9' + copy(campoLivre,1,5);
  c1 := c1 + Mod10(c1);
  insert('. ',c1,6);

  c2 := copy(campoLivre,6,10);
  c2 := c2 + Mod10(c2);
  insert('. ',c2,6);

  c3 := copy(campoLivre,16,10);
  c3 := c3 + Mod10(c3);
  insert('. ',c3,6);

  c4 := DVCodigoBarras;

  c5 := Formata_Tamanho_String(StringReplace(StringReplace(formatFloat('0.,00',valorCR),'.','',[]),',','',[]),14,'E','0');

  CodBarrasNumerico := c1 + ' ' + c2 + ' ' + c3 + ' ' + c4 + ' ' + c5;

  nossoNumero := nossoNumero + '-' + Mod11(nossoNumero,7);

  CDSBoleto.INSERT;

  CDSBoleto.FieldBYname('EMPRESA'      ).AsVariant := empresa;
  CDSBoleto.FieldBYname('DATA'         ).AsVariant := now;
  CDSBoleto.FieldBYname('DOC'          ).AsVariant := Doc;
  CDSBoleto.FieldBYname('VENCTO'       ).AsVariant := venctoCR;
  CDSBoleto.FieldBYname('AG_CODIGO_CED').AsVariant := agenciaCodCendente;
  CDSBoleto.FieldBYname('NOSSO_NUMERO' ).AsVariant := nossoNumero;
  CDSBoleto.FieldBYname('VALOR'        ).AsVariant := valorCR;
  CDSBoleto.FieldBYname('CLIENTE'      ).AsVariant := clienteNome;
  CDSBoleto.FieldBYname('TIPO_CLIENTE'      ).AsVariant := TIPOCLIENTE;
  CDSBoleto.FieldBYname('CPF_CNPJ'     ).AsVariant := cnpj;
  CDSBoleto.FieldBYname('END1'         ).AsVariant := clienteEndereco1;
  CDSBoleto.FieldBYname('END2'         ).AsVariant := clienteEndereco2;

  CDSBoleto.FieldBYname('CODIGOBARRADIGITAVEL').AsVariant := CodBarrasNumerico;
  CDSBoleto.FieldBYname('CODIGOBARRA'         ).AsVariant := CodBarras;
  CDSBoleto.FieldBYname('MSN'         ).AsVariant := MSN;


  CDSBoleto.post;

end;

procedure TFBoletoBradesco.CriaCDS;
begin

 if CDSBoleto.Active then // se estiver ativo...
  begin
    CDSBoleto.EmptyDataSet;
    CDSBoleto.Close;
  end;

  CDSBoleto.FieldDefs.Clear;


  CDSBoleto.FieldDefs.Add('EMPRESA'      ,ftString, 100);   // 1
  CDSBoleto.FieldDefs.Add('DATA'         ,ftDateTime, 0);   // 2
  CDSBoleto.FieldDefs.Add('DOC'          ,ftString,  20);   // 3
  CDSBoleto.FieldDefs.Add('VENCTO'       ,ftDateTime, 0);   // 4
  CDSBoleto.FieldDefs.Add('AG_CODIGO_CED',ftString,  30);   // 5
  CDSBoleto.FieldDefs.Add('NOSSO_NUMERO' ,ftString,  20);   // 6
  CDSBoleto.FieldDefs.Add('VALOR'        ,ftFloat,    0);   // 7
  CDSBoleto.FieldDefs.Add('CLIENTE'      ,ftString, 200);   // 8
  CDSBoleto.FieldDefs.Add('TIPO_CLIENTE'      ,ftString, 200);   // 8
  CDSBoleto.FieldDefs.Add('CPF_CNPJ'     ,ftString,  20); // 9
  CDSBoleto.FieldDefs.Add('END1'         ,ftString, 100);   // 10
  CDSBoleto.FieldDefs.Add('END2'         ,ftString, 100);   // 11
  CDSBoleto.FieldDefs.Add('CODIGOBARRADIGITAVEL',ftString, 100);   // 12
  CDSBoleto.FieldDefs.Add('CODIGOBARRA'         ,ftString, 100);   // 13
  CDSBoleto.FieldDefs.Add('MSN'         ,ftString, 255);   // 14



  CDSBoleto.CreateDataSet;

//  CDSBoleto.IndexFieldNames := 'ITENSNF_CODIGO';

  CDSBoleto.Close;

  CDSBoleto.OPEN;

  TCurrencyField(CDSBoleto.Fieldbyname('VALOR')).DisplayFormat := '0.,00';

end;

procedure TFBoletoBradesco.RLLabel44BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin
  text := formatdatetime('dd/mm/yyyy',now);
end;

procedure TFBoletoBradesco.RLLabel51BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin
  text := formatdatetime('dd/mm/yyyy',now);
end;

procedure TFBoletoBradesco.RLMemo1BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin
  text := msn;
end;

procedure TFBoletoBradesco.RLMemo2BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin
  text := msn;
end;

procedure TFBoletoBradesco.RLDBText14BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin

  IF CDSBoleto.FIELDbyname('TIPO_CLIENTE').AsInteger = 0 then
  begin

    TEXT := 'CPF: ' + TEXT;
    EXIT;

  end;

  IF CDSBoleto.FIELDbyname('TIPO_CLIENTE').AsInteger = 1 then
  begin

    TEXT := 'CNPJ: ' + TEXT;
    EXIT;

  end;
  
end;

procedure TFBoletoBradesco.RLDBText18BeforePrint(Sender: TObject;
  var Text: String; var PrintIt: Boolean);
begin
  IF CDSBoleto.FIELDbyname('TIPO_CLIENTE').AsInteger = 0 then
  begin

    TEXT := 'CPF: ' + TEXT;
    EXIT;

  end;

  IF CDSBoleto.FIELDbyname('TIPO_CLIENTE').AsInteger = 1 then
  begin

    TEXT := 'CNPJ: ' + TEXT;
    EXIT;

  end;
end;

end.
